package com.dyzx.hbb.modules.fcxs.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.fcxs.entity.FcxsCustomer;
import com.dyzx.hbb.modules.fcxs.entity.FcxsHouse;
import com.dyzx.hbb.modules.fcxs.query.FcxsCustomerQuery;
import com.dyzx.hbb.modules.fcxs.service.FcxsCustomerService;
import com.dyzx.hbb.modules.fcxs.vo.FcxsCustomerVO;
import com.dyzx.hbb.modules.fcxs.vo.FcxsCustomerUVO;
import com.dyzx.hbb.common.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 客户管理控制器
 * 
 * 功能说明：负责处理客户信息的创建、查询、更新和删除等操作
 * 主要职责：
 * - 提供客户信息的CRUD接口
 * - 支持按条件查询客户列表
 * - 提供客户简易列表查询接口（用于下拉选择）
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "02.客户管理", description = "客户管理相关接口，包括客户信息的创建、查询、更新和删除等操作")
@RestController
@RequestMapping("/fcxs/customer")
public class FcxsCustomerController extends BaseValidController<
    FcxsCustomerService,
    FcxsCustomer,
    FcxsCustomerQuery,
    FcxsCustomerVO,
    FcxsCustomerUVO
> {
    @Autowired
    private FcxsCustomerService fcxsCustomerService;

    /**
     * 获取基础服务类
     * @return FcxsCustomerService实例
     */
    @Override
    protected FcxsCustomerService getBaseService() {
        return fcxsCustomerService;
    }

    /**
     * 构建查询条件包装器
     * @param query 查询参数
     * @return 查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<FcxsCustomer> queryWrapper(FcxsCustomerQuery query) {
        MyLambdaQueryWrapper<FcxsCustomer> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsCustomer::getId)
                .select(FcxsCustomer::getName)
                .select(FcxsCustomer::getPhone)
                .select(FcxsCustomer::getCardNum)
                .select(FcxsCustomer::getBirthday)
                .select(FcxsCustomer::getAge)
                .imageUrl(FcxsCustomer::getCardFront).as("card_front_url")
                .imageUrl(FcxsCustomer::getCardBack).as("card_back_url")
                .select(FcxsCustomer::getSex)
                .select(FcxsCustomer::getRemark)
                .select(FcxsCustomer::getStatus)
                .select(FcxsCustomer::getCreateDate)
                .select(FcxsCustomer::getUpdateDate)
                .organName(FcxsCustomer::getOrganId).as("organ_name")
                .userName(FcxsCustomer::getOperatorId).as("operator_name");
        if (query != null){
            queryWrapper.like(StringUtils.isNotBlank(query.getName()), FcxsCustomer::getName, query.getName())
                    .like(StringUtils.isNotBlank(query.getPhone()), FcxsCustomer::getPhone, query.getPhone())
                    .like(StringUtils.isNotBlank(query.getCard_num()), FcxsCustomer::getCardNum, query.getCard_num())
                    .like(query.getSex()!=null, FcxsCustomer::getSex, query.getSex())
                    .eq(query.getStatus()!=null, FcxsCustomer::getStatus, query.getStatus())
                    .dateBetween(StringUtils.isNotBlank(query.getCreate_date()), FcxsCustomer::getCreateDate, DateFormat.DATE, query.getCreate_date());
        }
        return queryWrapper;
    }

    /**
     * 构建详情查询条件包装器
     * @return 详情查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<FcxsCustomer> queryInfoWrapper() {
        MyLambdaQueryWrapper<FcxsCustomer> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsCustomer::getId)
                .select(FcxsCustomer::getName)
                .select(FcxsCustomer::getPhone)
                .select(FcxsCustomer::getCardNum)
                .select(FcxsCustomer::getBirthday)
                .select(FcxsCustomer::getAge)
                .imageUrl(FcxsCustomer::getCardFront).as("card_front_url")
                .imageUrl(FcxsCustomer::getCardBack).as("card_back_url")
                .select(FcxsCustomer::getSex)
                .select(FcxsCustomer::getRemark)
                .select(FcxsCustomer::getStatus)
                .select(FcxsCustomer::getCreateDate)
                .select(FcxsCustomer::getUpdateDate)
                .organName(FcxsCustomer::getOrganId).as("organ_name")
                .userName(FcxsCustomer::getOperatorId).as("operator_name");
        return queryWrapper;
    }
    /**
     * 获取客户简易列表
     * @return 客户简易列表，包含ID和名称
     */
    @GetMapping("/mini_list")
    @Operation(summary = "获取客户简易列表", description = "获取所有客户的简易信息，仅包含ID和名称，用于下拉选择")
    public Result<List<Map<String, Object>>> miniList() {
        MyLambdaQueryWrapper<FcxsCustomer> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(FcxsCustomer::getId, FcxsCustomer::getName);
        wrapper.orderByDesc(FcxsCustomer::getUpdateDate);


        List<Map<String, Object>> list = getBaseService().listMaps(wrapper);
        return list.isEmpty() ? Result.error(404, "暂无数据") : Result.ok(list);
    }

    @Override
    protected FcxsCustomer addValid(FcxsCustomerVO entity) {
    return null;
    }

    @Override
    protected FcxsCustomer editValid(FcxsCustomerUVO entity) {
        FcxsCustomer data = new FcxsCustomer();
        BeanUtils.copyProperties(entity, data);
        data.setCardBack(null);
        data.setCardFront(null);
        System.out.println(data.toString());
        return data;
    }
}
